package service.impl;

import common.Constants.ServiceConstant;
import common.Util.MD5Util;
import service.TokenService;

import java.util.HashMap;

/**
 * Created by lxl on 2015/10/30.
 */
public class TokenServiceImpl implements TokenService {

    private HashMap<String , String> tokenMap = new HashMap<String, String>();

    @Override
    public int verifyToken(String token) {

        if( token == null || tokenMap.get(token) == null ){

            return ServiceConstant.VERIFY_ERROR ;

        }
        else {

            return ServiceConstant.VERIFY_SUCCES ;

        }
    }

    @Override
    public String toToken(String username) {

        String token = MD5Util.toMD5(username) ;

        if( token == null ){

            return ServiceConstant.VERIFY_TOKEN_ERROR ;

        }else {

            tokenMap.put(token,username) ;

            return token ;

        }

    }

    @Override
    public String getUserName(String token) {
        return (String)tokenMap.get(token);
    }
}
